package top.ajun.mall.ware.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.ajun.common.utils.PageUtils;
import top.ajun.common.utils.Query;
import top.ajun.common.utils.R;
import top.ajun.mall.ware.dao.WareSkuDao;
import top.ajun.mall.ware.entity.WareSkuEntity;
import top.ajun.mall.ware.feign.ProductFeignService;
import top.ajun.mall.ware.service.WareSkuService;

import java.util.List;
import java.util.Map;

@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareSkuEntity> page = this.page(
            new Query<WareSkuEntity>().getPage(params),
            Wrappers.query()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        LambdaQueryWrapper<WareSkuEntity> wrapper = Wrappers.lambdaQuery();

        // skuId 商品id
        String skuId = (String) params.get("skuId");
        if (StrUtil.isNotEmpty(skuId)) {
            wrapper.eq(WareSkuEntity::getSkuId, skuId);
        }

        // wareId 仓库id
        String wareId = (String) params.get("wareId");
        if (StrUtil.isNotEmpty(wareId)) {
            wrapper.eq(WareSkuEntity::getWareId, wareId);
        }

        IPage<WareSkuEntity> page = this.page(new Query<WareSkuEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        // 判断: 如果还没有此库存记录，则是新增
        List<WareSkuEntity> wareSkuList = this.list(
            Wrappers.<WareSkuEntity>lambdaQuery()
                .eq(WareSkuEntity::getSkuId, skuId)
                .eq(WareSkuEntity::getWareId, wareId)
        );
        if (CollUtil.isEmpty(wareSkuList)) {
            WareSkuEntity wareSku = new WareSkuEntity();
            wareSku.setSkuId(skuId);
            wareSku.setWareId(wareId);
            wareSku.setStock(skuNum);
            wareSku.setStockLocked(0);

            // 远程查询 sku 名称并设置
            try {
                R resp = productFeignService.info(skuId);
                @SuppressWarnings("unchecked")
                Map<String, Object> data = (Map<String, Object>) resp.get("skuInfo");
                if (resp.getCode() == 0) {
                    wareSku.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception ignored) {

            }
            this.save(wareSku);
        } else {
            // 否则更新
            this.baseMapper.addStock(skuId, wareId, skuNum);
        }
    }

}